package org.findbugs.report.entity;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Node;
import org.findbugs.report.util.XPathExpressions;

public class PackageStats {

    private String pack;
    private int bugs;
    private int size;
    private List<ClassStats> classStats;

    public static List<PackageStats> getPackageStats(Document findbugsDocument) {
        List<PackageStats> packageStatsList = new LinkedList<PackageStats>();
        List<Node> packageStatsNodes = findbugsDocument.selectNodes(XPathExpressions.PACKAGE_STATS);
        Iterator<Node> it = packageStatsNodes.iterator();
        while (it.hasNext()) {
            Node packageNode = it.next();
            PackageStats packageStats = new PackageStats();
            packageStats.setBugs(Integer.parseInt(packageNode.selectSingleNode("@total_bugs").getText()));
            packageStats.setPack(packageNode.selectSingleNode("@package").getText());
            packageStats.setSize(Integer.parseInt(packageNode.selectSingleNode("@total_size").getText()));
            packageStats.setClassStats(ClassStats.getClassStats(findbugsDocument, packageStats.getPack()));
            packageStatsList.add(packageStats);
        }
        return packageStatsList;
    }

    public List<ClassStats> getClassStats() {
        return classStats;
    }

    public void setClassStats(List<ClassStats> classStats) {
        this.classStats = classStats;
    }

    public String getPack() {
        return pack;
    }

    public int getBugs() {
        return bugs;
    }

    public int getSize() {
        return size;
    }

    public void setPack(String pack) {
        this.pack = pack;
    }

    public void setBugs(int bugs) {
        this.bugs = bugs;
    }

    public void setSize(int size) {
        this.size = size;
    }

}
